@extends('console.common.framework') 
@section('heads')
<script>
    var projects = '{!! json_encode($projects) !!}'
    var projectid = "{{$data['project']['id']}}"
    var categoryid = "{{$data['category']['id']}}"

    let inputTr = `
  <tr>
  <td class="form-group">
      <input type="text" name="input_param[]" value="" placeholder="参数名" class="form-control">
  </td>
  <td class="form-group">
      <select name="input_type[]" class="form-control">
        @foreach(config('param.types') as $type)
            <option value="{{$type}}">{{$type}}</option>
        @endforeach
      </select>
  </td>
  <td class="form-group">
      <input type="text" name="input_limited[]" value="不限" placeholder="长度" class="form-control">
  </td>
  <td class="form-group">
      <select name="input_required[]" class="form-control">
              <option value="1" selected>是</option>
              <option value="0">否</option>
        </select>
  </td>
  <td class="form-group">
      <input type="text" name="input_description[]" placeholder="参数描述" class="form-control">
  </td>
  <td class="form-group text-center">
      <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendInputTr()"></span>
      <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
  </td>
</tr>
`

let outputTr = `
  <tr>
  <td class="form-group">
      <input type="text" name="output_param[]" value="" placeholder="参数名" class="form-control">
  </td>
  <td class="form-group">
      <select name="output_type[]" class="form-control">
        @foreach(config('param.types') as $type)
            <option value="{{$type}}">{{$type}}</option>
        @endforeach
      </select>
  </td>
  <td class="form-group">
      <input type="text" name="output_description[]" placeholder="参数描述" class="form-control">
  </td>
  <td class="form-group text-center">
      <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendOutputTr()"></span>
      <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
  </td>
</tr>
  `

let errorTr = `
<tr>
    <td class="form-group">
        <input type="text" name="error_code[]" value="" placeholder="错误代码" class="form-control">
    </td>
    <td class="form-group">
        <input type="text" name="error_description[]" placeholder="错误描述" class="form-control">
    </td>
    <td class="form-group text-center">
        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendErrorTr()"></span>
        <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
    </td>
</tr>
  `
</script>
</script>
@endsection
 
@section('content')
<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">接口管理</h3>
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                接口编辑
            </div>
            <!-- /.panel-heading -->
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-12">
                        <form role="form" id="api-edit-form">
                            {{ csrf_field() }}
                            <input type="hidden" value="{{$data['id']}}" name="id">
                            <div class="form-group col-lg-6">
                                <label>所属项目</label>
                                <select class="form-control" name="project_id" id="project-select">
                                        @foreach ($projects as $li)
                                            @if($data['project']['id'] == $li['id'])
                                                <option value="{{$li['id']}}" selected>{{$li['title']}} | {{$li['vname']}}</option>
                                            @else
                                                <option value="{{$li['id']}}">{{$li['title']}} | {{$li['vname']}}</option>
                                            @endif
                                        @endforeach
                                    </select>
                            </div>
                            <div class="form-group col-lg-6">
                                <label>所属分类</label>
                                <select class="form-control" name="category_id" id="category-select">
                                    @if($data['project']['id'])
                                    @foreach ($projects as $li)
                                        @if($data['project']['id'] == $li['id'])
                                            @foreach ($li['categorys'] as $cat)
                                            @if($data['category']['id'] == $cat['id'])
                                                <option value="{{$cat['id']}}" selected>{{$cat['title']}}</option>
                                            @else
                                                <option value="{{$cat['id']}}">{{$cat['title']}}</option>
                                            @endif
                                            @endforeach
                                        @endif
                                    @endforeach
                                    @endif
                                    </select>
                            </div>
                            <div class="form-group col-lg-6">
                                <label>接口名称</label>
                                <input class="form-control" type="text" name="title" value="{{$data['title']}}" placeholder="分类标题">
                            </div>
                            <div class="form-group col-lg-6">
                                <label>接口vname</label>
                                <input class="form-control" type="text" name="vname" value="{{$data['vname']}}" placeholder="接口vname">
                            </div>
                            <div class="form-group col-lg-6">
                                <label>接口地址</label>
                                <input class="form-control" type="text" name="url" value="{{$data['url']}}" placeholder="接口地址">
                            </div>
                            <div class="form-group col-lg-6">
                                <label>功能简述</label>
                                <input class="form-control" type="text" name="description" value="{{$data['description']}}" placeholder="功能简述">
                            </div>
                            <div class="form-group col-lg-12">
                                <div class="row">
                                    <label class="col-lg-6">接口描述（支持markdown语法）</label>
                                    <label class="col-lg-6">预览</label>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6 col-sm-6 col-md-6">
                                        <textarea class="form-control" id="md" rows="8" name="intro" placeholder="接口描述">{{$data['intro']}}</textarea>
                                    </div>
                                    <div class="col-lg-6 col-sm-6 col-md-6" id="html"></div>
                                </div>
                            </div>
                            <div class="form-group col-lg-12">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        输入参数
                                    </div>
                                    <div class="panel-body">
                                        <table class="table table-hover" id="input-table">
                                            <thead>
                                                <tr>
                                                    <th class="text-center col-lg-2">参数名</th>
                                                    <th class="text-center col-lg-1">类型</th>
                                                    <th class="text-center col-lg-1">长度</th>
                                                    <th class="text-center col-lg-1">是否必须</th>
                                                    <th class="text-center col-lg-5">描述</th>
                                                    <th class="text-center col-lg-2">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @if(!empty($data['inputs']) && $data['inputs'] != '[]')
                                                @foreach($data['inputs'] as $input)
                                                <tr id="inputparam-{{$input['id']}}">
                                                    <input type="hidden" name="input_param_id[]" value="{{$input['id']}}">
                                                    <td class="form-group">
                                                        <input type="text" name="input_param[]" value="{{$input['param']}}" placeholder="参数名" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="input_type[]" class="form-control">
                                                            @foreach(config('param.types') as $type)
                                                            @if($type == $input['type'])
                                                            <option value="{{$type}}" selected>{{$type}}</option>
                                                            @else
                                                            <option value="{{$type}}">{{$type}}</option>
                                                            @endif
                                                        
                                                        @endforeach
                                                        </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="input_limited[]" value="{{$input['limited']}}" placeholder="长度" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="input_required[]" class="form-control">
                                                            <option value="1" @if(1 == $input['required']) selected @endif>是</option>
                                                            <option value="0" @if(0 == $input['required']) selected @endif>否</option>
                                                        </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="input_description[]" placeholder="参数描述" class="form-control" value="{{$input['description']}}">
                                                    </td>
                                                    <td class="form-group text-center">
                                                        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendInputTr()"></span>
                                                        <span class="fa fa-lg fa-trash-o del-span fa-action input-del" title="删除" data-id="{{$input['id']}}"></span>
                                                    </td>
                                                </tr>
                                                @endforeach
                                                @else
                                                <tr>
                                                    <td class="form-group">
                                                        <input type="text" name="input_param[]" value="" placeholder="参数名" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="input_type[]" class="form-control">
                                                            @foreach(config('param.types') as $type)
                                                                <option value="{{$type}}">{{$type}}</option>
                                                            @endforeach
                                                        </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="input_limited[]" value="不限" placeholder="长度" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="input_required[]" class="form-control">
                                                            <option value="1" selected>是</option>
                                                            <option value="0">否</option>
                                                        </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="input_description[]" placeholder="参数描述" class="form-control">
                                                    </td>
                                                    <td class="form-group text-center">
                                                        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendInputTr()"></span>
                                                        <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
                                                    </td>
                                                </tr>
                                                @endif
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-12">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        输出参数
                                    </div>
                                    <div class="panel-body">
                                        <table class="table table-hover" id="output-table">
                                            <thead>
                                                <tr>
                                                    <th class="text-center col-lg-2">参数名</th>
                                                    <th class="text-center col-lg-2">类型</th>
                                                    <th class="text-center col-lg-6">描述</th>
                                                    <th class="text-center col-lg-2">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @if(!empty($data['outputs']) && $data['outputs'] != '[]')
                                                @foreach($data['outputs'] as $output)
                                                <tr id="outputparam-{{$output['id']}}">
                                                    <input type="hidden" name="output_param_id[]" value="{{$output['id']}}">
                                                    <td class="form-group">
                                                        <input type="text" name="output_param[]" value="{{$output['param']}}" placeholder="参数名" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="output_type[]" class="form-control">
                                                                    @foreach(config('param.types') as $type)
                                                                        @if($type == $output['type'])
                                                                        <option value="{{$type}}" selected>{{$type}}</option>
                                                                        @else
                                                                        <option value="{{$type}}">{{$type}}</option>
                                                                        @endif
                                                                    
                                                                    @endforeach
                                                                    </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="output_description[]" placeholder="参数描述" class="form-control" value="{{$output['description']}}">
                                                    </td>
                                                    <td class="form-group text-center">
                                                        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendOutputTr()"></span>
                                                    <span class="fa fa-lg fa-trash-o del-span fa-action output-del" title="删除" data-id="{{$output['id']}}"></span>
                                                    </td>
                                                </tr>
                                                @endforeach
                                                @else
                                                <tr>
                                                    <td class="form-group">
                                                        <input type="text" name="output_param[]" value="" placeholder="参数名" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <select name="output_type[]" class="form-control">
                                                                @foreach(config('param.types') as $type)
                                                                    <option value="{{$type}}">{{$type}}</option>
                                                                @endforeach
                                                                </select>
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="output_description[]" placeholder="参数描述" class="form-control">
                                                    </td>
                                                    <td class="form-group text-center">
                                                        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendOutputTr()"></span>
                                                        <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
                                                    </td>
                                                </tr>
                                                @endif
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-12">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        错误码
                                    </div>
                                    <div class="panel-body">
                                        <table class="table table-hover" id="error-table">
                                            <thead>
                                                <tr>
                                                    <th class="text-center col-lg-3">错误代码</th>
                                                    <th class="text-center col-lg-7">错误描述</th>
                                                    <th class="text-center col-lg-2">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @if(!empty($data['errors']) && $data['errors'] != '[]')
                                                @foreach($data['errors'] as $error)
                                                <tr id="apierror-{{$error['id']}}">
                                                    <input type="hidden" name="error_code_id[]" value="{{ $error['id'] }}">
                                                        <td class="form-group">
                                                            <input type="text" name="error_code[]" value="{{ $error['code'] }}" placeholder="错误代码" class="form-control">
                                                        </td>
                                                        <td class="form-group">
                                                            <input type="text" name="error_description[]" value="{{ $error['description'] }}" placeholder="错误描述" class="form-control">
                                                        </td>
                                                        <td class="form-group text-center">
                                                            <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendErrorTr()"></span>
                                                            <span class="fa fa-lg fa-trash-o del-span fa-action apierror-del" title="删除" data-id="{{$error['id']}}"></span>
                                                        </td>
                                                    </tr>
                                                @endforeach
                                                @else
                                                <tr>
                                                    <td class="form-group">
                                                        <input type="text" name="error_code[]" value="" placeholder="错误代码" class="form-control">
                                                    </td>
                                                    <td class="form-group">
                                                        <input type="text" name="error_description[]" placeholder="错误描述" class="form-control">
                                                    </td>
                                                    <td class="form-group text-center">
                                                        <span class="fa fa-lg fa-plus-square-o fa-action" title="新增" onclick="fnAppendErrorTr()"></span>
                                                        <span class="fa fa-lg fa-trash-o del-span fa-action" title="删除" onclick="fnRemoveTr(this)"></span>
                                                    </td>
                                                </tr>
                                                @endif
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-6">
                                <label>输入示例</label>
                                <textarea class="form-control" rows="8" name="input_example" placeholder="输入示例">{{$data['input_example']}}</textarea>
                            </div>
                            <div class="form-group col-lg-6">
                                <label>输出示例</label>
                                <textarea class="form-control" rows="8" name="output_example" placeholder="输出示例">{{$data['output_example']}}</textarea>
                            </div>
                            <div class="form-group col-lg-12">
                                <label style="margin-right:25px;">接口状态</label>
                                <label class="radio-inline">
                                    <input type="radio" name="status" value="1" @if('1' == $data['status']) checked @endif>显示
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" name="status" value="0" @if('0' == $data['status']) checked @endif>隐藏
                                </label>
                            </div>
                            <div class="form-group col-lg-12">
                                <button type="button" class="btn btn-primary submit">修改</button>
                            </div>
                        </form>
                    </div>
                </div>
                <!-- /.table-responsive -->
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
</div>
@endsection
 
@section('scripts')
<script src="{{ env('APP_STATIC') }}console/js/api.js"></script>
@endsection